package my.org.swing.animate;

import java.awt.AlphaComposite;
import java.awt.Graphics2D;

public class TranslateActor implements Transformer {

    private boolean transparent = true;

    public static final int CenterExpand = 1;
    public static final int CenterShrink = 2;

    public static final int TopMoving = 3;
    public static final int BottomMoving = 4;

    public static final int LeftMoving = 5;
    public static final int RightMoving = 6;

    private int mode;

    public TranslateActor(int mode) {
        this.mode = mode;
    }

    @Override
    public void transform(Graphics2D g1, Graphics2D g2, double fraction, float width, float height) {
        float ratio = (float) fraction;

        if (transparent) {
            AlphaComposite c = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, ratio);
            g2.setComposite(c);
            g1.setComposite(c.derive(1 - ratio));
        }

        if (mode == CenterExpand) {
            if (ratio > 0) {
                g2.translate(width * (1 - ratio) / 2, height * (1 - ratio) / 2);
                g2.scale(ratio, ratio);
            }

            // 这里是消失的效果，放大目标组件的同时，缩小源组件
//            if (ratio < 1) {
//                g1.translate(width * ratio / 2, height * ratio / 2);
//                g1.scale(1 - ratio, 1 - ratio);
//            }

//            g1.setComposite(AlphaComposite.Dst); // 消失
        } else if (mode == CenterShrink) {
            if (ratio < 1) {
                g1.translate(width * ratio / 2, height * ratio / 2);
                g1.scale(1 - ratio, 1 - ratio);
            }

//            if (ratio > 0) {
//                g2.translate(width * (1 - ratio) / 2, height * (1 - ratio) / 2);
//                g2.scale(ratio, ratio);
//            }
//            g2.setComposite(AlphaComposite.Dst); // 消失
        } else if (mode == TopMoving) {
            g1.translate(0, -height * ratio);
            g2.translate(0, height * (1 - ratio));
        } else if (mode == LeftMoving) {
            g1.translate(-width * ratio, 0);
            g2.translate(width * (1 - ratio), 0);
        } else if (mode == BottomMoving) {
            g1.translate(0, height * ratio);
            g2.translate(0, -height * (1 - ratio));
        } else if (mode == RightMoving) {
            g1.translate(width * ratio, 0);
            g2.translate(-width * (1 - ratio), 0);
        }
    }
}
